package com.i2bt.app.files.model;

import com.i2bt.common.AndroidRestContract;

import com.i2bt.app.BaseActivity;
import com.i2bt.app.BaseFragment;
import com.i2bt.app.BaseJsonModel;
import com.i2bt.app.files.FilesFragment;
import com.i2bt.bean.FileBean;
import com.i2bt.bean.FileInfoBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FilesJsonHandle extends BaseJsonModel{
	
	public FilesJsonHandle(BaseActivity activity) {
		super(activity);
	}

	public FilesJsonHandle(BaseFragment fragment) {
		super(fragment);
	}

	/**
	 *  在这里写打包的逻辑
	 * @throws JSONException 
	 */
	@Override
	protected String logicToWrap() throws JSONException {


		switch (beanParam.getAct()) {
		
		case AndroidRestContract.GET_HOMEBOX_LIST : {
			jsonStrParam = wrapGetHomeBoxList();
			break;
		}

		//获取资源目录
		case AndroidRestContract.GET_FILES : {
			jsonStrParam = wrapGetFiles();
			break;
		}

		//删除多个资源
		case AndroidRestContract.DELETE_MULT_FILES : {
			jsonStrParam = wrapDeleteFiles();
			break;
		}

		//修改资源名称
		case AndroidRestContract.RENAME_FILES : {
			jsonStrParam = wrapRenameFile();
			break;
		}

		//新建文件夹
		case AndroidRestContract.CREATE_FILES : {
			jsonStrParam = wrapNewFile();
			break;
		}
		
		case AndroidRestContract.DOWNLOAD_FILES : {
			jsonStrParam = wrapDownloadFiles();
			break;
		}

		}
		
		return jsonStrParam;
		
	}
	
	/**
	 * 打包获取homebox列表
	 * @return
	 * @throws JSONException
	 */
	private String wrapGetHomeBoxList() throws JSONException {
		wrapCommonJson();
		return jsonParam.toString();
	}
	
	/**
	 * 把获取目录的bean打包成json
	 * @return 打包后的json
	 * @throws JSONException
	 */
	private String wrapGetFiles() throws JSONException {
		
		wrapCommonJson();
		
		String parentFolder = "";
		try {
			parentFolder = URLEncoder.encode(
					((FileBean)beanParam).getParentFolder(), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		jsonParam.put(AndroidRestContract.PARENT_FOLODER, parentFolder);
		
		return jsonParam.toString();
	}
	
	/**
	 * 打包删除文件的json
	 * @return 打包后的json
	 * @throws JSONException 
	 */
	private String wrapDeleteFiles() throws JSONException {
		
		wrapCommonJson();
		jsonParam.put(AndroidRestContract.FILE_PATHS, strsToJsonArray(
				((FileBean)beanParam).getRemoveFilePaths()));
		
		return jsonParam.toString();
		
	}
	
	/**
	 * 打包修改文件名称的json
	 * @return
	 * @throws JSONException
	 */
	private String wrapRenameFile() throws JSONException {
		
		wrapCommonJson();
		try {
			jsonParam.put(AndroidRestContract.PREVIOUS_PATH,
					URLEncoder.encode( ((FileBean)beanParam).getPreviousPath(), "utf-8" )
					)
			.put(AndroidRestContract.NEW_PATH, 
					URLEncoder.encode( ((FileBean)beanParam).getNewPath(), "utf-8" )
					);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return jsonParam.toString();
	}
	
	/**
	 * 打包新建文件夹
	 * @return
	 * @throws JSONException
	 */
	private String wrapNewFile() throws JSONException {
		
		wrapCommonJson();
		try {
			jsonParam.put(AndroidRestContract.PARENT_PATH, 
					URLEncoder.encode( ((FileBean) beanParam).getParentPath(), "utf-8" )
					)
			.put(AndroidRestContract.FOLDER_NAME, 
					URLEncoder.encode( ((FileBean) beanParam).getFolderName(), "utf-8" )
					);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return jsonParam.toString();
	}
	
	private String wrapDownloadFiles() throws JSONException {
		
		wrapCommonJson();
		FileBean fileBean = (FileBean) beanParam;
		jsonParam.put(AndroidRestContract.FILE_PATHS,
				strsToJsonArray(fileBean.getDownloadFiles()));
		
		return jsonParam.toString();
		
	}

	/**
	 * 在这里调用把json解包出来的
	 * @throws JSONException 
	 */
	@Override
	protected void logicToUnwrap(boolean isSuc, String result) throws JSONException {
		
		if (isSuc) {
			
			jsonParam = new JSONObject(result);

			if (jsonParam.getBoolean(AndroidRestContract.SUCCEED)) {
				FilesFragment filesFragment = (FilesFragment)fragment;

				switch (jsonParam.getInt(AndroidRestContract.ACTION)) {
				
				//获取homebox列表
				case AndroidRestContract.GET_HOMEBOX_LIST : {
					filesFragment.setHomeboxMap(unwrapGetHomeBoxList());
					filesFragment.getFilesRequest();
					break;
				}

				//获取资源
				case AndroidRestContract.GET_FILES : {
					filesFragment.getFilesResponse(unwrapGetFiles());
					break;
				}

				//删除1个或多个资源
				case AndroidRestContract.DELETE_MULT_FILES : {
					filesFragment.deleteFilesResponse();
					break;
				}

				//创建文件夹
				case AndroidRestContract.CREATE_FILES : {
					filesFragment.createFilesResponse();
					break;
				}

				//重命名资源
				case AndroidRestContract.RENAME_FILES : {
					filesFragment.renameFilesResponse();
					break;
				}
				
				case AndroidRestContract.DOWNLOAD_FILES : {
					String ip =  jsonParam.getString("ip");
					int port = jsonParam.getInt("port");
					filesFragment.downloadFilesResponse(ip, port);
					break;
				}

				}
			}
			else {
				fragment.toastHandler(jsonParam.
						getString(AndroidRestContract.MESSAGE));
			}
		}
		else {
			fragment.toastHandler(result);
		}

	}
	
	/**
	 * 解包获取homebox列表
	 * @return
	 * @throws JSONException
	 */
	private Map<String, String> unwrapGetHomeBoxList() throws JSONException {
		Map<String, String> homeboxMap = new HashMap<String, String>();
		JSONArray homeboxJsonArray = jsonParam.getJSONArray(AndroidRestContract.HOMEBOX_LIST);
		for (int i = 0; i < homeboxJsonArray.length(); i++) {
			JSONObject homeboxJsonObject = homeboxJsonArray.getJSONObject(i);
			homeboxMap.put(homeboxJsonObject.getString(AndroidRestContract.HOMEBOX_ID)
					, homeboxJsonObject.getString(AndroidRestContract.HOMEBOX_NAME));
		}
		
		return homeboxMap;
	}
	
	/**
	 * 解包获取我的目录
	 * @throws JSONException
	 */
	private List<FileInfoBean> unwrapGetFiles() throws JSONException {
		System.out.println(jsonParam.toString());
		
		List<FileInfoBean> fileInfoList = new ArrayList<FileInfoBean>();
		JSONArray jsonArrayTemp = jsonParam.getJSONArray(
				((FileBean)beanParam).getParentFolder());
		for (int i = 0; i < jsonArrayTemp.length(); i++) {
			fileInfoList.add( wrapFileBean(jsonArrayTemp.getJSONArray(i)) );
		}
		
		return fileInfoList;
		
	}
	
	/**
	 * 把我的目录每一个数组都解包成bean
	 * @param jsonArray 我的目录json数组
	 * @return
	 * @throws JSONException
	 */
	private FileInfoBean wrapFileBean(JSONArray jsonArray) throws JSONException {
		FileInfoBean fileInfoBean = new FileInfoBean();
		fileInfoBean.setFileName(jsonArray.getString(0));
		long fileLength = jsonArray.getLong(1);
		if (fileLength != 0) {
			fileInfoBean.setFile(true);
		}
		fileInfoBean.setFileLength(fileLength);
		return fileInfoBean;
	}
	
}
